<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of poi
 *
 * @author pudy
 */
class Poi extends CI_Controller {

    function add() {
        $data['title'] = 'Add New POI';
        $data['main_content'] = 'poi/add_poi_view';
        $user_id = $this->session->userdata('user_id');
        $data['user_id'] = $user_id;

        $this->load->model('categoryModel', 'categoryModel');
        $getReturnCategories = $this->categoryModel->getCategories();
        $categories = array();
        foreach ($getReturnCategories as $category) {
            $categories[$category->id] = $category->name;
        }
        $data['categories'] = $categories;

        $this->load->view('includes/template', $data);
    }

    function add_submit() {
        $user_id = $this->session->userdata('user_id');
        $title = $this->input->post('title');
        $description = $this->input->post('description');
        $latitude = $this->input->post('latitude');
        $longitude = $this->input->post('longitude');
        $category_id = $this->input->post('category_id');
        
        //retrieve all POIs
        $this->load->model('poiModel', 'poiModel');
        $options = array(
            'columnSelect' => 'title, latitude, longitude'
        );
        $getAllPois = $this->poiModel->getPois($options);

        if (is_bool($getAllPois)) {
            //add ke database karena belum ada POI di database
            $options = array(
                'user_id' => $user_id,
                'title' => $title,
                'description' => $description,
                'latitude' => $latitude,
                'longitude' => $longitude,
                'category_id' => $category_id
            );
            $this->load->model('poiModel', 'poiModel');
            $getReturnInsert = $this->poiModel->addPoi($options);

            $message = 'New POI has been added.';
            $this->session->set_flashdata('message', $message);
            redirect('/home', 'refresh');
        } else {
            $suspiciouspoi = array();
            foreach ($getAllPois as $poi) {
                $lat1 = $poi->latitude;
                $lon1 = $poi->longitude;
                $lat2 = doubleval($latitude);
                $lon2 = doubleval($longitude);

                $dLat = deg2rad($lat1 - $lat2);
                $dLon = deg2rad($lon1 - $lon2);

                $a = (sin($dLat / 2) * sin($dLat / 2)) + (sin($dLon / 2) * sin($dLon / 2) * cos($lat1) * cos($lat2));
                $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
                $d = 6371000 * $c;

                if ($d <= 500) {
                    $suspiciouspoi[] = $poi;
                }
            }
            
            if (count($suspiciouspoi) > 0) {
                //buka halaman verifikasi
                $data['title'] = 'Add POI - Verify';
                $data['main_content'] = 'poi/verify_add_poi_view';
                $data['suspiciouspoi'] = $suspiciouspoi;
                $data['poi_title'] = $title;
                $data['description'] = $description;
                $data['latitude'] = $latitude;
                $data['longitude'] = $longitude;
                $data['category_id'] = $category_id;
                
                $this->load->view('includes/template', $data);
            } else {
                //add ke database karena belum ada POI di database yg berada dalam radius 500 meter dari POI masukan
                $options = array(
                    'user_id' => $user_id,
                    'title' => $title,
                    'description' => $description,
                    'latitude' => $latitude,
                    'longitude' => $longitude,
                    'category_id' => $category_id
                );
                $this->load->model('poiModel', 'poiModel');
                $getReturnInsert = $this->poiModel->addPoi($options);

                $message = 'New POI has been added.';
                $this->session->set_flashdata('message', $message);
                redirect('/home', 'refresh');
            }
        }
    }
    
    function add_verify() {
        $user_id = $this->session->userdata('user_id');
        $title = $this->input->post('title');
        $description = $this->input->post('description');
        $latitude = $this->input->post('latitude');
        $longitude = $this->input->post('longitude');
        $category_id = $this->input->post('category_id');

        $options = array(
            'user_id' => $user_id,
            'title' => $title,
            'description' => $description,
            'latitude' => $latitude,
            'longitude' => $longitude,
            'category_id' => $category_id
        );
        $this->load->model('poiModel', 'poiModel');
        $getReturnInsert = $this->poiModel->addPoi($options);

        $message = 'New POI has been added.';
        $this->session->set_flashdata('message', $message);
        redirect('/home', 'refresh');
    }
    
    function add_unverify() {
        $message = 'Failed to add new POI.';
        $this->session->set_flashdata('message', $message);
        redirect('/home', 'refresh');
    }

    function view($poi_id) {
        $this->load->model('poiModel', 'poiModel');
        $options = array('id' => $poi_id);
        $getReturnPoi = $this->poiModel->getPois($options);

        $category_id = $getReturnPoi[0]->category_id;
        $this->load->model('categoryModel', 'categoryModel');
        $options2 = array('id' => $category_id,
            'columnSelect' => 'name');
        $getReturnCategory = $this->categoryModel->getCategories($options2);

        $poi = array(
            'title' => $getReturnPoi[0]->title,
            'description' => $getReturnPoi[0]->description,
            'latitude' => $getReturnPoi[0]->latitude,
            'longitude' => $getReturnPoi[0]->longitude,
            'category' => $getReturnCategory[0]->name
        );

        $data['title'] = $poi['title'] . ' - View';
        $data['main_content'] = 'poi/detail_poi_view';
        $data['poi'] = $poi;

        $this->load->view('includes/template', $data);
    }

    function edit($poi_id) {
        $this->load->model('poiModel', 'poiModel');
        $options = array('id' => $poi_id);
        $getReturnPoi = $this->poiModel->getPois($options);

        $poi = array(
            'id' => $poi_id,
            'title' => $getReturnPoi[0]->title,
            'description' => $getReturnPoi[0]->description,
            'latitude' => $getReturnPoi[0]->latitude,
            'longitude' => $getReturnPoi[0]->longitude,
            'category_id' => $getReturnPoi[0]->category_id
        );

        $this->load->model('categoryModel', 'categoryModel');
        $getReturnCategories = $this->categoryModel->getCategories();
        $categories = array();
        foreach ($getReturnCategories as $category) {
            $categories[$category->id] = $category->name;
        }

        $data['title'] = $poi['title'] . ' - Edit';
        $data['main_content'] = 'poi/edit_poi_view';
        $data['poi'] = $poi;
        $data['categories'] = $categories;

        $this->load->view('includes/template', $data);
    }

    function edit_submit($poi_id) {
        $title = $this->input->post('title');
        $description = $this->input->post('description');
        $latitude = $this->input->post('latitude');
        $longitude = $this->input->post('longitude');
        $category_id = $this->input->post('category_id');

        $options = array(
            'id' => $poi_id,
            'title' => $title,
            'description' => $description,
            'latitude' => $latitude,
            'longitude' => $longitude,
            'category_id' => $category_id
        );
        $this->load->model('poiModel', 'poiModel');
        $getReturnInsert = $this->poiModel->updatePoi($options);

        $message = 'Your POI has been changed.';
        $this->session->set_flashdata('message', $message);
        redirect('/home', 'refresh');
    }

    function delete($poi_id) {
        $this->load->model('poiModel', 'poiModel');
        $options = array(
            'id' => $poi_id,
            'columnSelect' => 'title'
        );
        $getReturnTitle = $this->poiModel->getPois($options);
        $poi_title = $getReturnTitle[0]->title;
        
        $data['title'] = 'Delete POI - Verify';
        $data['main_content'] = 'poi/verify_delete_poi_view';
        $data['poi_id'] = $poi_id;
        $data['poi_title'] = $poi_title;

        $this->load->view('includes/template', $data);
    }
    
    function delete_verify($poi_id) {
        $this->load->model('poiModel', 'poiModel');
        $options = array('id' => $poi_id);
        
        $getReturnDelete = $this->poiModel->deletePoi($options);
        if ($getReturnDelete) {
            $message = 'POI berhasil dihapus';
        } else {
            $message = 'POI gagal dihapus';
        }
        $this->session->set_flashdata('message', $message);
        redirect('/home', 'refresh');
    }
    
    function delete_unverify() {
        $message = 'POI batal dihapus';
        $this->session->set_flashdata('message', $message);
        redirect('/home', 'refresh');
    }

}

?>
